package com.jyx.util.date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

public class CalendarUtil {

	public static void main(String[] args) {
		new CalendarUtil().testName();
	}
	
	
	@Test
	public void testName(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, 7);  
		System.out.println("max:"+calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.DATE, 1);  
		calendar.roll(Calendar.DATE, -1);  
        int maxDate = calendar.get(Calendar.DATE); 
        System.out.println("maxDate:"+maxDate);
		/*
		System.out.println(calendar.getTime());
		calendar.set(Calendar.MONTH, 6);//.setTime(new Date("2017-6-6"));
		System.out.println(calendar.get(Calendar.MONTH));
		
		calendar.set(Calendar.YEAR, 2017);  
		calendar.set(Calendar.MONTH, 7);  
		calendar.set(Calendar.DATE, 1);  
		calendar.roll(Calendar.DATE, -1);  
        int maxDate = calendar.get(Calendar.DATE);
        System.out.println("maxDate:"+maxDate);
        
		System.out.println(calendar.getMaximum(calendar.DAY_OF_MONTH));//.getActualMaximum(calendar.DAY_OF_MONTH));
		  */
	        int currentMaxDays = getCurrentMonthDay();  
	          
	        int maxDaysByDate = getDaysByYearMonth(2017, 6);  
	          
	        String week = getDayOfWeekByDate("2012-12-25");  
	          
	        System.out.println("本月天数：" + currentMaxDays);  
	        System.out.println("2012年11月天数：" + maxDaysByDate);  
	        System.out.println("2012-12-25是：" + week);  
	    }  
	      
	    /** 
	     * 获取当月的 天数 
	     * */  
	    public static int getCurrentMonthDay() {  
	          
	        Calendar a = Calendar.getInstance();  
	        a.set(Calendar.DATE, 1);  
	        a.roll(Calendar.DATE, -1);  
	        int maxDate = a.get(Calendar.DATE);  
	        return maxDate;  
	    }  
	  
	    /** 
	     * 根据年 月 获取对应的月份 天数 
	     * */  
	    public static int getDaysByYearMonth(int year, int month) {  
	          
	        Calendar a = Calendar.getInstance();  
	        a.set(Calendar.YEAR, year);  
	        a.set(Calendar.MONTH, month - 1);  
	        a.set(Calendar.DATE, 1);  
	        a.roll(Calendar.DATE, -1);  
	        int maxDate = a.get(Calendar.DATE);  
	        return maxDate;  
	    }  
	      
	    /** 
	     * 根据日期 找到对应日期的 星期 
	     */  
	    public static String getDayOfWeekByDate(String date) {  
	        String dayOfweek = "-1";  
	        try {  
	            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
	            Date myDate = myFormatter.parse(date);  
	            SimpleDateFormat formatter = new SimpleDateFormat("E");  
	            String str = formatter.format(myDate);  
	            dayOfweek = str;  
	              
	        } catch (Exception e) {  
	            System.out.println("错误!");  
	        }  
	        return dayOfweek;  
	    }
	
}
